﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

namespace Model
{
    public class TypeProcess
    {
        public int ID { get; set; }
        public string Name { get; set; }

	    public TypeProcess()
	    {
            this.ID = 0;
            this.Name = "";
	    }
     }
    public class TypeProcessManager
    {   
        public List<TypeProcess> GetAll()
        {
            List<TypeProcess> result = new List<TypeProcess>();
            string sqlQuery = String.Format("select * from TypeProcess");
            SqlConnection connection = new SqlConnection(Utilitarios.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();
            TypeProcess process = null;
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    process = new TypeProcess();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.Name = dataReader["Name"].ToString();
                    result.Add(process);
                }
            }
            return result;
        }

        public int Save(TypeProcess obj)
        {
            string createQuery = String.Format("Insert into TypeProcess (Name) Values('{0}');"
                + "Select @@Identity", obj.Name);

            SqlConnection connection = new SqlConnection(Utilitarios.ConnKLegal);
            connection.Open();
            SqlCommand command = null;

            command = new SqlCommand(createQuery, connection);

            int saveID = 0;
            try
            {
                var commandResult = command.ExecuteScalar();
                if (commandResult != null)
                {
                    saveID = Convert.ToInt32(commandResult);
                }               
            }
            catch (Exception ex)
            {
                
            }

            command.Dispose();
            connection.Close();
            connection.Dispose();

            return saveID;
        }

        public int Update(TypeProcess obj)
        {
            string updateQuery = String.Format("Update TypeProcess SET Name='{0}' Where ID = {1};",
               obj.Name, obj.ID);

            SqlConnection connection = new SqlConnection(Utilitarios.ConnKLegal);
            connection.Open();
            SqlCommand command = null;
            command = new SqlCommand(updateQuery, connection);

            int saveID = 0;
            try
            {
                var commandResult = command.ExecuteScalar();
                if (commandResult != null)
                {
                    saveID = Convert.ToInt32(commandResult);
                }
                else
                {
                saveID = obj.ID;
                }
            }
            catch (Exception ex)
            {

            }
            command.Dispose();
            connection.Close();
            connection.Dispose();

            return saveID;
        }

        public TypeProcess GetById(int articleId)
        {
            TypeProcess result = new TypeProcess();
            string sqlQuery = String.Format("select * from TypeProcess where ID={0}", articleId);
            SqlConnection connection = new SqlConnection(Utilitarios.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();

            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    result.ID = Convert.ToInt32(dataReader["ID"]);
                    result.Name = dataReader["Name"].ToString();                   
                }
            }

            return result;
        }

        public bool Delete(int articleId)
        {
            bool result = false;
            string sqlQuery = String.Format("delete from TypeProcess where ID = {0}", articleId);

            SqlConnection connection = new SqlConnection(Utilitarios.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            int rowsDeletedCount = command.ExecuteNonQuery();
            if (rowsDeletedCount != 0)
                result = true;
            command.Dispose();
            connection.Close();
            connection.Dispose();
            return result;
        }
    }
}